﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using GamePlugin.Logics.Core;
using System.Collections.ObjectModel;

namespace GamePlugin.Logics.Data
{
    public class DetailedPropertyTreeItem : DependencyObject
    {
        public List<string> GetList()
        {
            List<string> ret = new List<string>();
            if (this.Children.Count == 0)
            {
                if (this.IsChecked)
                    ret.Add(this.FullName);
            }
            else
            {
                foreach (var child in this.Children)
                {
                    List<string> childList = child.GetList();
                    for (int i = 0; i < childList.Count; i++)
                        ret.Add(childList[i]);
                }
            }
            return ret;
        }

        public void ClearAll()
        {
            this.IsChecked = false;
            foreach (var child in this.Children)
            {
                child.IsChecked = false;
            }
        }

        public void Enable(string value)
        {
            if (this.FullName == value)
                this.IsChecked = true;
            foreach (var child in this.Children)
            {
                child.Enable(value);
            }
        }

        public DetailedPropertyTreeItem Parent
        {
            get;
            set;
        }

        public string FullName
        {
            get
            {
                string value = this.Name;
                if (Parent != null)
                    value = Parent.FullName + this.Name;
                return value;
            }
        }

        public DetailedPropertyTreeItem(DetailedPropertyTreeItem parent, string name)
        {
            Parent = parent;
            this.Name = name;
            this.Children = new ObservableCollection<DetailedPropertyTreeItem>();
            if (parent != null)
                parent.Children.Add(this);
        }

        public static readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.Register("IsChecked", typeof(bool), typeof(DetailedPropertyTreeItem),
            new FrameworkPropertyMetadata(false, new PropertyChangedCallback(_OnIsCheckedChanged)));
        static void _OnIsCheckedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            DetailedPropertyTreeItem item = (DetailedPropertyTreeItem)sender;
            bool value = (bool)e.NewValue;
            foreach (var child in item.Children)
                child.IsChecked = value;
        }

        [Editable]
        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }

        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register("Name", typeof(string), typeof(DetailedPropertyTreeItem), null);
        [Editable]
        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        public static readonly DependencyProperty ChildrenProperty =
            DependencyProperty.Register("Children", typeof(ObservableCollection<DetailedPropertyTreeItem>), typeof(DetailedPropertyTreeItem), null);
        [Editable]
        public ObservableCollection<DetailedPropertyTreeItem> Children
        {
            get { return (ObservableCollection<DetailedPropertyTreeItem>)GetValue(ChildrenProperty); }
            set { SetValue(ChildrenProperty, value); }
        }
    }
}
